/*
 * UartDrv.c
 *
 *  Created on: Apr 6, 2015
 *      Author: ma.xw
 */
#include "UartDrv.h"
#include <S3C2440addr.h>
#include <commlib.h>

#define GPH2_3_MASK (0xF0)
#define MAX_SEND_LEN (10240)
#define MAX_RECV_LEN (4096)


void uart_init(unsigned int baud_rate) {

    //config GPF2 -> TXD0, GPF3 -> RXD0
    rGPHCON &= ~GPH2_3_MASK;
    rGPHCON |= 0xA0;

    rGPHUP = 0x00;//enable pull up
    rULCON0 = 0x3; //uart line control register,8 bit Character, 1 stop bit, no parity
    rUCON0 = 0x5;//PCLK(50MHz) as clock, send/receive by query or interupt
    rUFCON0  = 0x00;     // 不使用FIFO
    rUMCON0  = 0x00;     // 不使用流控

    //UBRDIVn = (int)( UART clock / ( buad rate x 16) ) –1
    //(unsigned short)(50000000 / (baud_rate * 16)) - 1;//baud=115200 -> 26.12->26=0x1A
    rUBRDIV0 = 26;

    rINTMSK &= ~(1 << 28); //clear mask bit of INT_UART0
    //rINTSUBMSK &= ~(3 << 0); //clear INTSUBMSK.bit[1:0] to enable INT_RXD0 and INT_TXD0
    rINTSUBMSK &= ~(1 << 0); //clear INTSUBMSK.bit[0] to enable INT_RXD0 (and disable INT_TXD0)
}

#define IS_SENDING (0 == (rUTRSTAT0 & (1 << 1)))
int uart_print_char(unsigned char c) {
    while (!(rUTRSTAT0 & 2)) {}
    rUTXH0 = c;
    return 0;
}

int uart_printk(const char* pbuf) {
    int i = 0;
    while(pbuf[i]) {
        while (!(rUTRSTAT0 & 2));
        rUTXH0 = pbuf[i];
        ++i;
    }
    return 0;
}
